<?php
// * 微信接口
require_once(dirname(__FILE__).'/config.php');
require_once(DEDEINC.'/datalistcp.class.php');
require_once(DEDEINC.'/common.func.php');
CheckPurview('plus_微信接口');
setcookie('ENV_GOBACK_URL',$dedeNowurl,time()+3600,'/');
if(empty($dopost)) $dopost = '';
$id = 1;
$ENV_GOBACK_URL = empty($_COOKIE['ENV_GOBACK_URL']) ? "weixin.php" : $_COOKIE['ENV_GOBACK_URL'];
if($dopost=='saveedit')
{
    $query = "UPDATE `#@__weixin`
     SET
	 url='$url',
     TOKEN='$TOKEN',
     AppId='$AppId',
     AppSecret='$AppSecret',
     ACCESS_TOKEN='$ACCESS_TOKEN',
     weixinid='$weixinid',
     weixinno='$weixinno',
     menu='$menu',
     msg='$msg',
	 searchrx='$searchrx',
	 menurx='$menurx'
     WHERE id='$id'
     ";
    $dsql->ExecuteNoneQuery($query);
    ShowMsg("成功更改微信接口配置！",$ENV_GOBACK_URL);
    exit();
}

if ($action=="do"){
	$ACCESS_TOKEN=get_access_token($appid,$secret);
	$query = "UPDATE `#@__weixin` SET ACCESS_TOKEN='$ACCESS_TOKEN' WHERE id='$id' ";
	$dsql->ExecuteNoneQuery($query);
	ShowMsg("通信完成，请查看操作结果是否成功！",$ENV_GOBACK_URL);
	exit();
	}

    //自定义菜单中获取access_token
    function get_access_token($appid,$secret){
		$url="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$secret;
		$json=http_request_json($url); //这个地方不能用file_get_contents
		$data=json_decode($json,true);
		if($data['access_token']){
			return $data['access_token'];
		}else{
			return "获取access_token错误";
		}		
	}
	//因为url是https 所有请求不能用file_get_contents,用curl请求json 数据
	function http_request_json($url){  
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL,$url);
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		$result = curl_exec($ch);
		curl_close($ch);
		return $result;  
	}

$row = $dsql->GetOne("SELECT * FROM `#@__weixin` WHERE id='$id'");
include DedeInclude('templets/weixin.htm');
?>